package streamer

import (
	"net/http"

	"github.com/EDDYCJY/go-gin-example/pkg/app"
	"github.com/EDDYCJY/go-gin-example/pkg/e"
	"github.com/EDDYCJY/go-gin-example/pkg/util"
	"github.com/EDDYCJY/go-gin-example/service/streamer_service"
	"github.com/gin-gonic/gin"
	"github.com/unknwon/com"
)

type GetStreamerVerify struct {
}

func GetStreamer(c *gin.Context) {
	var (
		appG = app.Gin{C: c}
		form GetStreamerVerify
	)
	_, errCode := app.BindAndValidMsg(c, &form)
	if errCode != nil {
		appG.ResponseMsg(errCode, nil)
		return
	}

	InstitutionID := c.MustGet("institution_id").(int)
	Service := streamer_service.GetStreamer{
		InstitutionID: InstitutionID,
		PageNum:       util.GetPage(c),
		PageSize:      util.GetLimit(c),
	}
	data, err := Service.GetStreamer()
	if err != nil {
		appG.ResponseMsg(err, nil)
		return
	}
	//异步日志
	appG.Response(http.StatusOK, e.SUCCESS, data)

}

type AddStreamerVerify struct {
	StreamerName   string `alias:"主播名称" form:"streamer_name"  json:"streamer_name" valid:"Required;"`
	StreamerMobile string `alias:"主播号码" form:"streamer_mobile"  json:"streamer_mobile" valid:"Required;"`
	TagIds         string `alias:"标签" form:"tag_ids"  json:"tag_ids" `
}

func AddStreamer(c *gin.Context) {
	var (
		appG = app.Gin{C: c}
		form AddStreamerVerify
	)
	_, errCode := app.BindAndValidMsg(c, &form)
	if errCode != nil {
		appG.ResponseMsg(errCode, nil)
		return
	}

	InstitutionID := c.MustGet("institution_id").(int)
	Service := streamer_service.AddStreamer{
		InstitutionID:  InstitutionID,
		StreamerName:   form.StreamerName,
		StreamerMobile: form.StreamerMobile,
		TagIds:         form.TagIds,
	}
	data, err := Service.AddStreamer()
	if err != nil {
		appG.ResponseMsg(err, nil)
		return
	}
	//异步日志
	appG.Response(http.StatusOK, e.SUCCESS, data)

}

type EditStreamerVerify struct {
	ID             int    `form:"id" valid:"Required;"`
	StreamerName   string `alias:"主播名称" form:"streamer_name"  json:"streamer_name" valid:"Required;"`
	StreamerMobile string `alias:"主播号码" form:"streamer_mobile"  json:"streamer_mobile" valid:"Required;"`
	TagIds         string `alias:"标签" form:"tag_ids"  json:"tag_ids" `
}

func EditStreamer(c *gin.Context) {
	var (
		appG = app.Gin{C: c}
		form = EditStreamerVerify{ID: com.StrTo(c.Param("id")).MustInt()}
	)
	_, errCode := app.BindAndValidMsg(c, &form)
	if errCode != nil {
		appG.ResponseMsg(errCode, nil)
		return
	}

	InstitutionID := c.MustGet("institution_id").(int)
	Service := streamer_service.EditStreamer{
		InstitutionID:  InstitutionID,
		StreamerID:     form.ID,
		StreamerName:   form.StreamerName,
		StreamerMobile: form.StreamerMobile,
		TagIds:         form.TagIds,
	}
	data, err := Service.EditStreamer()
	if err != nil {
		appG.ResponseMsg(err, nil)
		return
	}
	//异步日志
	appG.Response(http.StatusOK, e.SUCCESS, data)

}

type DeleteStreamerVerify struct {
	ID int `form:"id" valid:"Required;"`
}

func DeleteStreamer(c *gin.Context) {
	var (
		appG = app.Gin{C: c}
		form = DeleteStreamerVerify{ID: com.StrTo(c.Param("id")).MustInt()}
	)
	_, errCode := app.BindAndValidMsg(c, &form)
	if errCode != nil {
		appG.ResponseMsg(errCode, nil)
		return
	}

	InstitutionID := c.MustGet("institution_id").(int)
	Service := streamer_service.DeleteStreamer{
		InstitutionID: InstitutionID,
		StreamerID:    form.ID,
	}
	data, err := Service.DeleteStreamer()
	if err != nil {
		appG.ResponseMsg(err, nil)
		return
	}
	//异步日志
	appG.Response(http.StatusOK, e.SUCCESS, data)

}
